package com.hrp.book.adapter

import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.hrp.analyzer.model.Novel
import com.hrp.book.R
import com.hrp.book.databinding.ItemBookBinding

class SearchBookAdapter :
    BaseQuickAdapter<Novel, BaseDataBindingHolder<ItemBookBinding>>(R.layout.item_book) {
    override fun convert(holder: BaseDataBindingHolder<ItemBookBinding>, item: Novel) {
        holder.dataBinding?.run {
            Glide.with(cover)
                .load(item.coverUrl)
                .placeholder(R.drawable.ic_default_cover)
                .error(R.drawable.ic_default_cover)
                .into(cover)

            name.text = item.title
            introduce.text = item.introduce
            if (item.author != null || item.category != null) {
                when {
                    item.category == null -> {
                        categoryAndAuthor.text = item.author
                    }
                    item.author == null -> {
                        categoryAndAuthor.text = item.category
                    }
                    else -> {
                        categoryAndAuthor.text = item.category + " | " + item.author
                    }
                }
            } else {
                categoryAndAuthor.text = "null"
            }
        }
    }

    override fun onViewRecycled(holder: BaseDataBindingHolder<ItemBookBinding>) {
        super.onViewRecycled(holder)
        holder.dataBinding?.run {
            Glide.with(cover)
                .clear(cover)
        }
    }
}